package com.woniuxy.demo.epai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.demo.epai.dao.AuctionRecordMapper;
import com.woniuxy.demo.epai.dao.ProductMapper;
import com.woniuxy.demo.epai.model.*;
import com.woniuxy.demo.epai.dao.ProductRecordMapper;
import com.woniuxy.demo.epai.service.IOrderService;
import com.woniuxy.demo.epai.service.IProductRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.demo.epai.vo.ArecordVo;
import com.woniuxy.demo.epai.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.persistence.criteria.Order;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 拍卖记录表 服务实现类
 * </p>
 *
 * @author epai
 * @since 2021-04-12
 */
@Service
@Slf4j
public class ProductRecordServiceImpl implements IProductRecordService {
    @Autowired
    private ProductRecordMapper mapper;

    @Autowired
    private AuctionRecordMapper auctionRecordMapper;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ProductRecordMapper productRecordMapper;

    @Autowired
    private ProductMapper productMapper;

    private void updlost(ProductRecord productRecord){
        //结束时间
        final long end = productRecord.getDeadline().getTime();
        final Timer timer = new Timer();
        //计时结束时候，停止全部timer计时计划任务
        timer.schedule(new TimerTask() {
            public void run() {
                List<ArecordVo> arecordVoById = auctionRecordMapper.getArecordVoById(productRecord.getPid());
                log.info("查询出价表{}",arecordVoById);
                Product product = new Product();
                ProductRecord byPid = productRecordMapper.findByPid(productRecord.getPid());
                ArecordVo arecordVo = null;
                if(!ObjectUtils.isEmpty(arecordVoById)){
                    log.info("进入取最高价");
                    arecordVo = arecordVoById.get(arecordVoById.size()-1);
                }
                if (arecordVo==null){
                    log.info("进入流拍");
                    productRecord.setStatus(2);
                    mapper.save(productRecord);
                    product.setId(productRecord.getPid());
                    product.setStatus("STREAMING");
                    productMapper.updateById(product);
                }else {
                    productRecord.setProAucId(arecordVo.getId());
                    productRecord.setTransactionPrice(arecordVo.getMoney());
                    product.setId(productRecord.getPid());
                    product.setStatus("SELL_OUT");
                    productMapper.updateById(product);
                    mapper.save(productRecord);
                    orderService.createOrder(byPid.getId(),arecordVo.getUid());
                    log.info("进入创建订单");
                }

                timer.cancel();
            }
        }, new Date(end));
    }

    /**
     * 新增商品记录
     * @param productRecord
     * @return
     */
    @Override
    public void addProductRecord(ProductRecord productRecord) {
         mapper.save(productRecord);
         updlost(productRecord);
    }

    @Override
    public void updProductRecord(ProductRecord productRecord) {
        ProductRecord byPid = mapper.findByPid(productRecord.getPid());
        byPid.setHot(byPid.getHot()+1);
        mapper.save(byPid);
    }
}
